package com.spirit.dao.userdao;

import com.spirit.domain.user.Aderess;
import com.spirit.domain.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class UserDao {
    private static Map<Integer,User> userMap;

    private static int userId = 0;

    @Autowired
    private AderessDao aderessDao;

    public AderessDao getAderessDao() {
        return aderessDao;
    }

    public void setAderessDao(AderessDao aderessDao) {
        this.aderessDao = aderessDao;
    }

    static {
        userMap = new HashMap<Integer, User>();
        userId = ++ userId;
        userMap.put(userId,new User("jack","123456",21,"1",
                new Aderess("五龙口街","太原","山西",userId),userId));
        userId = ++ userId;
        userMap.put(userId,new User("rose","123456",20,"2",
                new Aderess("长风街","太原","山西",userId),userId));
        userId = ++ userId;
        userMap.put(userId,new User("mary","123456",19,"2",
                new Aderess("学府街","太原","山西",userId),userId));
        userId = ++ userId;
        userMap.put(userId,new User("allen","123456",3,"1",
                new Aderess("南中环街","太原","山西",userId),userId));
    }

    public Collection<User> listUsers(){
        return userMap.values();
    }

    public void addUser(User user) {
        userId = ++ userId;
        user.setId(userId);
        userMap.put(userId,user);
    }

    public void removeUser(Integer userId) {
        userMap.remove(userId);
    }

    public void updateUser(User user) {
        userMap.remove(user);
        user.setAderess(aderessDao.getAderess(user.getAderess().getId()));
        userMap.put(user.getId(),user);
    }

    public User getUser(Integer id) {
        return userMap.get(id);
    }
}
